package com.qdlc.p2p.manage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.TppInvestService;
import com.qdlc.p2p.biz.service.TppService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.Summary;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Tpp;
import com.qdlc.p2p.dal.model.TppModel;

/**
 * 后台第三方处理
 * 
 * @author ylx
 * @version 2.0
 * @date 2015年6月12日 上午10:00:51
 */
public class ManageTppAction extends BaseAction<TppModel> implements ModelDriven<TppModel> {
	private static final Logger logger = Logger.getLogger(ManageTppAction.class);
	
	@Resource
	private TppService tppService;
	@Resource
	private TppInvestService tppInvestService;

	
	private Map<String, Object> data;
	/**
	 * 调度任务管理界面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/tpp/tppDispatchTaskManage")
	public String tppDispatchTaskManage() throws Exception {
		
		return "tppDispatchTaskManage";
	}
	
	/**
	 * 调度任务列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/tpp/tppDispatchTaskList")
	public void tppDispatchTaskList() throws Exception {
		data = new HashMap<String, Object>();
		if (StringUtil.isBlank(paramString("status"))) {
			model.setStatus(99);
		}
		PageDataList<TppModel> pageList = tppService.pageDataList(model);
		int totalPage = pageList.getPage().getTotal();
		data.put("total", totalPage);
		data.put("rows", pageList.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 调度任务
	 * @throws Exception
	 */
	@Action("/modules/tpp/tppDispatchTask")
	public void tppDispatchTask() throws Exception {
		String[] ids = request.getParameterValues("ids");
		if (ids != null && ids.length > 0) {
			List<TppModel> taskList = new ArrayList<TppModel>();
			for (int i = 0; i < ids.length; i++) {
				int id = NumberUtil.getInt(ids[i]);
				logger.info("调度处理当前ID:" + id);
				if (id > 0) {
					Tpp tpp = tppService.getTppById(id);
					TppModel tppModel = TppModel.instance(tpp);
					tppModel.setSummary(Summary.getSummaryNameByType(tpp.getServiceType())+"(补单)");
					if (tpp.getStatus() != TppModel.STATUS_SUCEESS) {
						taskList.add(tppModel);
					}
				}
			}
			tppInvestService.tppDispatchTask(taskList);
		}
		printSuccess();
	}

	/**
	 * 导出Excel_获取列表信息
	 * @throws Exception
	 */
	@Action("/modules/tpp/exportExcelList")
	public void exportExcelList() throws Exception{
		model.setPage(1);
		model.setRows(5000);
		// 初始化status值为99
		if(model.getStatus()==0)
			model.setStatus(99);
		PageDataList<TppModel> pageDataList = tppService.pageDataList(model);

		String title = "调度任务Excel表";

		String[] hearders = new String[] { "收款方手机号", "收款方姓名", "接口类型", "付款方", "金额", "收款方", "操作类型","状态", "订单号", "备注","操作时间","标id" };// 表头数组
		String[] fields = new String[] { "toUserMobile", "toUserRealName", "tppTypeStr","userName", "money", "toUserName", "serviceTypeStr","statusStr", "orderId","respDesc","addTime","tppBorrowId"};// 对象属性数组
		TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
}
